<?php

namespace App\Http\Controllers;

use App\Answer;
use App\Msg;
use App\Zan;
use Illuminate\Http\Request;

class AnswerController extends Controller
{
    //
    public function create(Request $request, Answer $answer)
    {
        $this->validate($request,[
           'content'=>'required|min:3'
        ]);
        $answer->content = $request ->content;
        $answer->question_id = $request -> question_id;
        $answer->user_id = \Auth::id();
        $answer->save();
        $msg = new Msg();
        $msg->question_id = $answer->question->id;
        $msg->source_id = \Auth::id();
        $msg->target_id = $answer->question->user->id;
        $msg->operate = '回复';
        $msg->save();
        return [
            'error'=>0,
            'info'=>'回复成功',
        ];
    }
    public function zan(Request $request)
    {
        $answer = Answer::find($request->answer_id);
        $data = [
            'user_id'=>\Auth::id(),
            'answer_id'=>$request->answer_id,
        ];

        $res = Zan::firstOrCreate($data);
        $msg = new Msg();
        $msg->question_id = $answer->question->id;
        $msg->source_id = \Auth::id();
        $msg->target_id = $answer->user->id;
        $msg->operate = '点赞';
        $msg->save();
        if ($res){
            return [
                'error'=>0,
                'info'=>'点赞'
            ];
        }
    }
    public function caina(Answer $answer)
    {
        $answer->status = 1;
        $answer->save();
        $answer->question->status = 1;
        $answer->question->save();
        $msg = new Msg();
        $msg->question_id = $answer->question->id;
        $msg->source_id = \Auth::id();
        $msg->target_id = $answer->question->user->id;
        $msg->operate = '采纳';
        $msg->save();
        return[
          'error'=>0,
          'info'=>'采纳成功',
        ];
    }
}
